package com.itany.shop.controller;

import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

/**
 * @author 石小俊
 * @date 2023年07月06日 15:36
 */
@WebServlet("*.cart")
public class CartServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        switch (path){
            case "/add.cart":
                addCart(request,response);
                break;
            case "/modify.cart":
                modifyCart(request,response);
                break;
            case "/removeById.cart":
                removeById(request,response);
                break;
            case "/removeByIds.cart":
                removeByIds(request,response);
                break;
            case "/clear.cart":
                clear(request,response);
                break;
            default:
                PrintWriter out = response.getWriter();
                out.println("<h1 style='color:red;'>您输入的请求有误,请重新输入</h1>");
                break;
        }
    }

    protected void removeByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        String[] ids = request.getParameterValues("id");
        System.out.println("ids:"+ Arrays.toString(ids));

        try {
            for(String id : ids){
                cartService.removeCart(cart,id);
            }
            response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("cartMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        }
    }

    protected void modifyCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");

        Cart cart = (Cart) request.getSession().getAttribute("cart");
        String id = request.getParameter("id");
        String num = request.getParameter("num");

        try {
            cartService.modifyCart(cart,id,num);
            response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("cartMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("cartMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        }

    }

    protected void removeById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        String id = request.getParameter("id");

        try {
            cartService.removeCart(cart,id);
            response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("cartMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("cartMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        }

    }

    protected void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 清空购物车不是销毁,而是将购物车中的数据清空,购物车仍然存在
        // 可以创建一个初始化的购物车替换当前购物车
        request.getSession().setAttribute("cart", new Cart());
        response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
    }

    protected void addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");

        // 从session中取出购物车
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        // 获取请求参数
        String id = request.getParameter("id");

        try {
            cartService.addCart(cart,id);
            response.sendRedirect(request.getContextPath()+"/findAll.product");
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("productMsg",e.getMessage());
            request.getRequestDispatcher("/findAll.product").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("productMsg","服务器内部异常");
            request.getRequestDispatcher("/findAll.product").forward(request,response);
        }

    }
}
